const express = require("express")
const {returnData,handleAsyncError} = require("../utils/returnMsg")
const app = express();
const adminRouter = express.Router();
const {loginStrict} = require("../services/adminService")
//导入加密方法
const {encrypto} = require("../utils/crypto.js")

let cookieMsg 

adminRouter.post('/',handleAsyncError(async (req,res,next)=>{
//    console.log("cookie",req.cookie)
    // console.log("请求体",req.body)
    // console.log(req)
    console.log(req.body)
    const resp = await loginStrict(req.body.loginId,req.body.loginPwd);
   
    if(resp){
        const targetStr = encrypto(resp.id.toString());
        // 登录成功
        // 设置cookie
        // 使用设置cookie中间件 进行cookie的设置
        res.cookie("token",targetStr,{
            path: "/",
            maxAge: 7 * 24 * 3600 * 1000,
            domain:"localhost",
            httpOnly:false,
        
        })
        // 设置响应头
        // res.setHeader("token",targetStr)
        res.setHeader("set-cookie",`token=${targetStr}; path=/; domain=localhost; max-age=3600; `) 
    }
    return resp
}))
adminRouter.delete('/:id',handleAsyncError(async (req,res,next)=>{
    // 这是一个需要token的接口
    return 'need a token'
}))
adminRouter.get('/:id',handleAsyncError(async (req,res,next)=>{
    // console.log(req.cookie)
     // 这是一个不需要token的接口
    // console.log(req.cookies)// 这里就可以拿到cookie
    // { token: '4' }
    console.log("abc")
    console.log("xixi")
    return '测试cookie don\'t need token'
}))

module.exports = adminRouter